package com.iwhalecloud.bss.kite.cucc.order.accept.constructor.inst;

import com.google.common.collect.ImmutableMap;
import com.iwhalecloud.bss.kite.common.constant.TableConsts;
import com.iwhalecloud.bss.kite.cucc.order.accept.constructor.DataConstructorFactory;
import com.iwhalecloud.bss.kite.cucc.order.accept.constructor.DataConstructorManager;
import com.iwhalecloud.bss.kite.cucc.order.util.AcceptUtils;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.OfferInst;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.OfferObjInstRel;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.ActionType;
import com.iwhalecloud.bss.kite.manager.enums.InstType;
import com.iwhalecloud.bss.kite.manager.enums.RelType;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInstAcct;
import com.iwhalecloud.bss.kite.manager.inst.graph.impl.RelEdge;
import com.iwhalecloud.bss.kite.order.client.dto.OrderRequest;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 账户级销售品实例构造器
 */
@Component
public class OfferInstAcctRelConstructor extends InstConstructor<KiteOfferInstAcct, OfferObjInstRel> {

    @Override
    public String getTableName() {
        return TableConsts.OfferObjInstRel.getTableName();
    }

    @Override
    public OfferObjInstRel newInst(OrderRequest orderRequest, KiteOfferInstAcct kiteOfferInstAcct) {
        return null;
    }

    @Override
    public OfferObjInstRel build(OrderRequest orderRequest, String offerObjInstRelId, Map<String, Object> params) {
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        KiteOfferInst kiteOfferInst = querySceneInstService.getOfferInstByOfferInstAcctId(orderRequest.getCustId(),
            offerObjInstRelId);
        if (isIneffectiveInst(kiteOfferInst)) {
            return null;
        }

        KiteOfferInstAcct offerInstAcct = querySceneInstService.getOfferInstAcct(orderRequest.getCustId(),
            offerObjInstRelId);
        if (isIneffectiveInst(offerInstAcct)) {
            return null;
        }

        InstConstructor<KiteOfferInst, OfferInst> instConstructor = DataConstructorFactory
            .getInstConstructor(kiteOfferInst.getInstType());
        if (!instConstructor.contains(orderRequest, "", kiteOfferInst.getOfferInstId(), null)) {
            instConstructor.build(orderRequest, kiteOfferInst.getOfferInstId(),
                ImmutableMap.of(KiteInst.ACTION_TYPE, ActionType.M.name()));
        }
        else {
            DataConstructorManager.handleParentActionType(orderRequest, kiteOfferInst.getInstType(),
                kiteOfferInst.getOfferInstId());
        }

        if (isAddByCurrent(orderRequest, offerInstAcct)) {
            // 补充账户销售品实例明细新关系新增的变动信息
            AcceptUtils.addRelChange(orderRequest, kiteOfferInst.getOfferInstId(), kiteOfferInst.getInstType(),
                offerInstAcct.getAcctId(), InstType.OFFER_INST_ACCT, RelType.OFFER_OBJ_REL_TYPE_170000,
                KiteInst.ACTION_TYPE, "", ActionType.A.name());
        }

        RelEdge relEdge = new RelEdge();
        relEdge.setSourceInstId(kiteOfferInst.getInstId());
        relEdge.setSourceInstType(kiteOfferInst.getInstType());
        relEdge.setTargetInstId(offerInstAcct.getAcctId());
        relEdge.setTargetInstType(offerInstAcct.getInstType());
        relEdge.setRelType(RelType.OFFER_OBJ_REL_TYPE_170000);
        relEdge.setActionType(offerInstAcct.getActionType());
        RelConstructor<OfferObjInstRel> relConstructor = DataConstructorFactory
            .getRelConstructor(RelType.OFFER_OBJ_REL_TYPE);
        return relConstructor.build(orderRequest, relEdge,
            ImmutableMap.of(KEY_INST_REL_ID, offerInstAcct.getOfferObjInstRelId()));
    }
}
